
package com.segment.dcore.base.filter;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import com.segment.dcore.base.constants.FilterOrderConstants;

/**
 * ServiceFilter抽象实现
 *
 * Created by huapeng.hhp on 2017/2/27.
 */
public abstract class AbstractServiceFilter implements ServiceFilter, InitializingBean, DisposableBean {
	@Override
	public void destroy() throws Exception {
		getFilterRegister().filterDestroied(this);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		getFilterRegister().filterCreated(this);
	}

	@Override
	public int getOrder() {
		return FilterOrderConstants.SERVICE_DEFAULT_ORDER;
	}

	@Override
	public void doFilter(FilterChain filterChain, InvokeContext invokeContext) {
		try {
			before(invokeContext);
			filterChain.doFilter(invokeContext);
		} finally {
			after(invokeContext);
		}
	}

	/** 过滤器注册器 */
	protected abstract FilterRegister getFilterRegister();

	/**
	 *
	 * @param invokeContext
	 *            调用上下文
	 */
	protected abstract void before(InvokeContext invokeContext);

	/**
	 * @param invokeContext
	 *            调用上下文
	 */
	protected abstract void after(InvokeContext invokeContext);
}
